<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class file_image extends CI_Controller{
      protected $_templates;
      function __construct() {
          parent::__construct();
          $this->pre_message = "";
          $this->session->set_userdata(array('Url'=>uri_string()));
		  $this->lang->load('file_image');
          $this->load->model('file_image_model','file_image');
          $this->load->model('file_image_cat_model','file_image_cat');
		  $this->load->helper('upload_file');
      }   
	  function index(){
		  $this->list_file_image();
	  }
      function list_file_image(){
         
        /*Begin search*/
            $type = $this->input->post('type');
            $data['search_bar']=searchBar(array(),array('title'=>'Tiêu đề'),'file_image/list_file_image/');
            if($type=='ajax')
            {
                $arr_search['key_search'] = $this->input->post('key_search');
                $arr_search['field_search'] = $this->input->post('field_search');
                $this->session->set_userdata('key_search',$arr_search['key_search']);
                $this->session->set_userdata('field_search',$arr_search['field_search']);
               
            }
            else{
                $arr_search['key_search']=$this->session->userdata('key_search');
                $arr_search['field_search']=$this->session->userdata('field_search');
            }
            /*End  search*/
            $data['title'] = 'Danh sách hình ảnh';
            $data['add'] = 'file_image/add';
            $config['base_url'] = base_url().'file_image/list_file_image/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->file_image->get_num_data($arr_search);
            $config['total_rows']   =  $data['num'];
            $config['per_page']  =   '20';
            $config['uri_segment'] = 4;   
            $this->pagination->initialize($config);   
            $data['list'] =   $this->file_image->get_all_data($config['per_page'],(int)$this->uri->segment('4'),$arr_search);
            $data['pagination']    = $this->pagination->create_links();            
            $this->_templates['page'] = 'file_image/list_file_image';
           if($type=="ajax")
                echo $this->load->view($this->_templates['page'],$data,true);
            else
                    $this->site_lib->load($this->_templates['page'],$data); 
      }
      function add(){                         
            $data['title'] = 'Thêm hình ảnh';
            $data['opt_image_cat'] = $this->file_image->getAllImageCat();
            $this->form_validation->set_rules('file_image_cat_code',lang('file_image_cat'),'trim|required');
            $this->form_validation->set_rules('title',lang('file_image_name'),'trim|required');
            $this->form_validation->set_rules('title_en',lang('file_image_name'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 $uploadDir = "../uploads/media/images/";
				 $files = $_FILES['uploadfile'];
                 $img_name = settings_upload_avatar($files,$uploadDir);
				 if($img_name!='-1'){
					 $ext =$this->file_image->get_file_extension($files['name']);
					 $size = $files['size'];
					 $arr_image = array('file_name'=>$img_name,'ext'=>$ext,'size'=>$size);
					 $data = $this->build_data($_POST,1);
					 $data  = array_merge($data,$arr_image);
					 if($this->file_image->saveData(0,$data)){
						 $this->session->set_flashdata('message','Lưu thành công');
						 redirect('file_image/list_file_image');
					 }else{
						 
						 $this->pre_message = 'Lưu không thành công';
					 }
				 }else{
					  $this->pre_message = 'File không đúng định dạng, vui lòng tải lại';
				 }
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'file_image/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){                         
            $data['title'] = 'Sửa hình ảnh';
            $data['query'] = $this->file_image->get_object($id);
            $data['opt_image_cat'] = $this->file_image->getAllImageCat();
            $this->form_validation->set_rules('file_image_cat_code',lang('file_image_cat'),'trim|required');
            $this->form_validation->set_rules('title',lang('image_name'),'trim|required');
            $this->form_validation->set_rules('title_en',lang('image_name_en'),'trim|required');
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				$uploadDir = "../uploads/media/images/";
				$files = $_FILES['uploadfile'];
				if($files['name']!='')
					$img_name = settings_upload_avatar($files,$uploadDir);
				else
					$img_name =$data['query']->file_name; 
				if($img_name!='-1'){
					$arr_image = array('file_name'=>$img_name);	
					$ext =$this->file_image->get_file_extension($files['name']);
					 $size = $files['size'];
					 $arr_image = array('file_name'=>$img_name,'ext'=>$ext,'size'=>$size);
					$data = $this->build_data($_POST,0);
					$data  = array_merge($data,$arr_image);
					 if($this->file_image->saveData($id,$data)){
						 $this->session->set_flashdata('message','Lưu thành công');
						 redirect('file_image/list_file_image');
					 }else{
						 $this->pre_message = 'Lưu không thành công';
					 }
				}else{
					$this->pre_message = 'File không đúng định dạng, vui lòng tải lại';
				}
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'file_image/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->file_image->deleted($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('file_image/list_file_image/'.$page);
    }
    function dels(){
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->file_image->deleted($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('file_image/list_file_image/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
		$dataUpdate["dir_name"] ="uploads/images/images_lib/";
		$dataUpdate["file_image_cat_code"]=trim($data["file_image_cat_code"]);
		$dataUpdate["title"]=trim($data["title"]);
		$dataUpdate["title_en"]=trim($data["title_en"]);
		$dataUpdate["description"]=trim($data["description"]);
		$dataUpdate["description_en"]=trim($data["description_en"]);
		$dataUpdate["seo_keyword"]=trim($data["seo_keyword"]);
		$dataUpdate["seo_keyword_en"]=trim($data["seo_keyword_en"]);
		if($flag==1)
			$dataUpdate["dt_create"] = date('Y-m-d H:i:s');
        if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}
	

  }
?>
